package com.zhhtech.zhh.ui.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.ViewCompat
import com.zhhtech.zhh.ui.R
import com.zhhtech.zhh.ui.listener.OnValidationResultListener
import com.zhhtech.zhh.ui.utils.VariableViewUtils
import me.jessyan.autosize.utils.AutoSizeUtils

class CommonButton : AppCompatTextView, View.OnClickListener, OnValidationResultListener {
    companion object {
        const val CB_NORMAL = 1 // 直接使用主色调非空心
        const val CB_STROKE_TEXT = 2 // 字体色色空心
        const val CB_RED = 3 // 红色字体色色非空心
        const val CB_STROKE_NORMAL = 4 // 正常边框
        const val CB_STROKE_HINT = 5 // 浅色边框
        const val CB_AUTH = 6 // 认证
        const val CB_TAG = 7 // 标签
        const val CB_WELFARE = 8 // 福利
        const val CB_STROKE_LINE = 9
    }

    private var mOnClickListener: OnClickListener? = null
    private var defaultEnable = true// 是否活跃
    private var mVariableViewUtils: VariableViewUtils? = null
    private var normalDrawable: Drawable? = null
    private var enableFalseDrawable: Drawable? = null
    private var mCBStyle = CB_NORMAL
    private var defaultTextColor: Int = -1
    private var stroke: Int = 1
    private var textsize: Float = 16f
    private var radius: Float = 6f

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setOnClickListener(this)
        init(attrs)
    }

    fun init(attrs: AttributeSet?) {
        stroke = AutoSizeUtils.dp2px(context, stroke.toFloat())
        textsize = AutoSizeUtils.dp2px(context, textsize).toFloat()
        radius = AutoSizeUtils.dp2px(context, radius).toFloat()
        attrs?.let {
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonButton)
            mCBStyle = typedArray.getInt(R.styleable.CommonButton_cb_style, CB_NORMAL)
            textsize = typedArray.getDimension(R.styleable.CommonButton_but_text_size, textSize)
            radius = typedArray.getDimension(R.styleable.CommonButton_radius, radius)
            typedArray.recycle()
        }
        typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        changeStyle()
        enableFalseDrawable = GradientDrawable().apply {
            setColor(Color.parseColor("#E6E6E6"))
            cornerRadius = radius
        }
        setTextSize(TypedValue.COMPLEX_UNIT_PX, textsize)
        gravity = Gravity.CENTER
    }

    fun changeStyle(cbStyle: Int = mCBStyle) {
        mCBStyle = cbStyle
        when (cbStyle) {
            CB_NORMAL -> {
                normalDrawable = GradientDrawable(
                    GradientDrawable.Orientation.LEFT_RIGHT,
                    intArrayOf(Color.parseColor("#5a8ef5"), Color.parseColor("#2d67f5"))
                ).apply {
                    cornerRadius = radius
//                    setColor(context.getColor(R.color.colorPrimary))
                }
                defaultTextColor = context.getColor(R.color.white)
            }
            CB_RED -> {
                normalDrawable = GradientDrawable().apply {
                    setColor(context.getColor(R.color.button_red))
                    cornerRadius = radius
                }
                defaultTextColor = context.getColor(R.color.white)
            }
            CB_STROKE_TEXT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, context.getColor(R.color.text_color))
                }
                defaultTextColor = context.getColor(R.color.text_color)
            }
            CB_STROKE_NORMAL -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, context.getColor(R.color.main_color))
                }
                defaultTextColor = context.getColor(R.color.main_color)
            }
            CB_STROKE_HINT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, context.getColor(R.color.text_login_hint))
                }
                defaultTextColor = context.getColor(R.color.text_login_hint)
            }
            CB_AUTH -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(context.getColor(R.color.text_tag));
                }
                defaultTextColor = context.getColor(R.color.text_hint_color_2)
            }
            CB_TAG -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(context.getColor(R.color.text_tag));
                }
                defaultTextColor = context.getColor(R.color.text_auth)
            }
            CB_WELFARE -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(context.getColor(R.color.text_color))
                }
                defaultTextColor = context.getColor(R.color.welfare_num)
            }
            CB_STROKE_LINE -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, context.getColor(R.color.line))
                    setColor(context.getColor(R.color.white))
                }
                defaultTextColor = context.getColor(R.color.text_color)
            }
        }
        setTextColor(defaultTextColor)
        ViewCompat.setBackground(this, if (defaultEnable) normalDrawable else enableFalseDrawable)
    }

    fun getCbStyle() = mCBStyle

    override fun setOnClickListener(l: OnClickListener?) {
        if (this == l)
            super.setOnClickListener(this)
        else
            mOnClickListener = l
    }

    fun setVariableButtonUtils(variableViewUtils: VariableViewUtils) {
        mVariableViewUtils = variableViewUtils
    }

    override fun onClick(v: View?) {
        if (mOnClickListener != null && defaultEnable) {
            //if (BLUtils.notRepeatClick()) {
            if (mVariableViewUtils == null) {
                mOnClickListener?.onClick(v)
            } else if (mVariableViewUtils!!.clickValidator()) {
                mOnClickListener?.onClick(v)
            }
            //}
        }
    }

    override fun enable(enable: Boolean): Boolean {
        if (enable && !defaultEnable) {
            ViewCompat.setBackground(this, normalDrawable)
            setTextColor(defaultTextColor)
        }
        if (!enable && defaultEnable) {
            ViewCompat.setBackground(this, enableFalseDrawable)
            setTextColor(context.getColor(R.color.text_color))
        }
        defaultEnable = enable
        return defaultEnable
    }
}